<template>
  <div id="login" style="width: 100%; height: 100vh; overflow: hidden;">
    <div style="width: 400px; margin: 100px auto">
      <div align="center">
        <img alt="Vue logo" src="../assets/img1.png">
      </div>
      <div style="color: #cccccc; font-size: 40px; text-align: center; padding: 30px 0">UVA Manager</div>
      <el-form style="background-color: #cccccc79 ;padding: 50px" ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="用户名" prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="密码" prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item style="position:relative;text-align: center;">
          <el-button style="width: 45%;" type="primary" @click="login" >登 录</el-button>
          <el-button style="width: 45%;" type="primary" @click="register">注 册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    sessionStorage.removeItem("user")
  },
  methods: {
    register(){
      this.$router.push("/register")
    },
    login() {

      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/login", this.form).then(res => {
            console.log(res.data);
            if (res.data.code === 0) {
              this.$message({
                type: "success",
                message: "登录成功"
              })
              sessionStorage.setItem("user", JSON.stringify(res.data.user))  // 缓存用户信息
              this.$router.push("/")  //登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({
                type: "error",
                message: res.data.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
#login{
  background: url("../assets/background2.jpeg");
  background-size: 100% 100%;
  width:100%;
  height:100%;
  position:fixed;
}
</style>
